package com.own.business.system.user.business.service.impl;

import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.own.business.system.user.business.service.SystemTripartiteAccountService;
import com.own.business.system.user.entity.bo.SystemTripartiteAccountBo;
import com.own.business.system.user.entity.map.SystemTripartiteAccountMapVo;
import com.own.business.system.user.entity.po.SystemTripartiteAccount;
import com.own.business.system.user.entity.query.SystemTripartiteAccountQuery;
import com.own.business.system.user.entity.vo.SystemTripartiteAccountVo;
import com.own.business.system.user.mapper.SystemTripartiteAccountMapper;
import com.own.component.common.base.service.impl.AbstractBaseBindTwoService;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * SystemTripartiteAccountServiceImpl
 *
 * @author chenxueli
 * @date 2021/10/07 16:30:18
 */
@Slf4j
@Service
public class SystemTripartiteAccountServiceImpl extends AbstractBaseBindTwoService<
        SystemTripartiteAccount,
        SystemTripartiteAccountBo,
        SystemTripartiteAccountVo,
        SystemTripartiteAccountMapVo,
        SystemTripartiteAccountQuery,
        SystemTripartiteAccountMapper
        > implements SystemTripartiteAccountService {

    @Resource
    public SystemTripartiteAccountMapper systemTripartiteAccountMapper;

    /**
     * 获取pk的属性
     *
     * @return pk的属性
     */
    @Override
    public SFunction<SystemTripartiteAccount, Long> getPkExtractor() {
        return SystemTripartiteAccount::getFkTargetId;
    }

    /**
     * 获取fk的属性
     *
     * @return fk的属性
     */
    @Override
    public SFunction<SystemTripartiteAccount, Long> getFkExtractor() {
        return SystemTripartiteAccount::getFkUserId;
    }

    /**
     * 创建新的对象
     *
     * @param pk 主id
     * @param fk 副id
     * @return 新对象
     */
    @Override
    public SystemTripartiteAccount buildPo(@NonNull Long pk, @NonNull Long fk) {
        return buildPo(pk, fk, "");
    }

    /**
     * 创建新的对象
     *
     * @param pk   主id
     * @param fk   副id
     * @param type 账号类型
     * @return 新对象
     */
    @Override
    public SystemTripartiteAccount buildPo(@NonNull Long pk, @NonNull Long fk, String type) {
        return new SystemTripartiteAccount(pk, fk, type);
    }

    /**
     * 保存新的数据
     *
     * @param pk   主id
     * @param fk   副id
     * @param type 账号类型
     * @return 新对象
     */
    @Override
    public SystemTripartiteAccount bindPo(@NonNull Long pk, @NonNull Long fk, String type) {
        var po = buildPo(pk, fk, type);
        systemTripartiteAccountMapper.insert(po);
        return po;
    }

    /**
     * 根据type等信息获取第三方账号信息
     *
     * @param fk   主id
     * @param type 账号类型
     * @return 对象信息
     */
    @Override
    public SystemTripartiteAccount getByType(Long fk, String type) {
        var wrapper = lambdaQueryWrapper().eq(SystemTripartiteAccount::getFkTargetId, fk)
                .eq(SystemTripartiteAccount::getType, type);
        return systemTripartiteAccountMapper.selectOne(wrapper);
    }
}
